import sys
import os
import time
import math
BASE_PATH = os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))
)
sys.path.insert(0,BASE_PATH)
from bin import get_Webdriver
from page_object.public import Public

class My_Data:
    def __init__(self):
        self.driver = get_Webdriver.Get_Webdriver().driver

    def jumptoMyDatapage(self):
        """进入我的数据"""
        curl = Public().getCurrentUrl()
        if '/mydata/main.html' not in curl:
            self.driver.click("link_text=>我的数据")
        else:
            self.driver.F5()


    def clicktopfolderByid(self, folderid='treeDemo_1'):
        """根据顶级文件夹id点击文件夹，treeDemo_1为默认文件夹"""
        self.driver.click('css=>#{}_span'.format(folderid))

    def clicktopfolderMoreByid(self, folderid='treeDemo_1'):
        """根据顶级文件夹id点击文件夹后more，treeDemo_1为默认文件夹"""
        self.driver.click('css=>#addBtn_{}'.format(folderid))

    def folderclickDel(self):
        """点击删除文件夹"""
        self.driver.click('css=>td[data-num="2"]')

    def getAllTopfolderList(self):
        """获取所有顶级文件夹的id"""
        folder_list = self.driver.get_attributes('css=>li[class="level0"]', 'id')
        return folder_list

    def getFnameByid(self, fid):
        """根据id获取文件夹名称"""
        fname = self.driver.get_text('css=>#{}_span'.format(fid))
        return fname

    def inputFoldername(self, fname):
        """输入文件夹的名称"""
        self.driver.clear('css=>#name')
        self.driver.type('css=>#name', fname)

    def clickAddSubmit(self):
        """输入文件夹名称时点击确定按钮"""
        self.driver.click('css=>#submit')

    def clickUploadSubmit(self):
        """上传文件时点击确定按钮"""
        self.driver.click('css=>#ctlBtn')

    def clickUploadSecretarySubmit(self):
        """上传文件时点击确定按钮"""
        self.driver.click('css=>#save')

    def clickAddCancle(self):
        """输入文件夹名称时点击取消按钮"""
        self.driver.click('css=>button[onclick="closeDlg()"]')

    def addFolder(self, foldername, topfolderid='treeDemo_1', location='same', clicksubmit='y'):
        """点击一个文件夹后的...按钮，添加文件夹，
        foldername：文件名
        topfolderid:源文件夹id
        location:同级或下级
        """
        self.clicktopfolderByid(topfolderid)
        time.sleep(0.2)
        self.clicktopfolderMoreByid(topfolderid)
        time.sleep(0.2)
        if location == 'same':
            timestap = self.folderclickAddSame()
            time.sleep(0.2)
            self.driver.switch_to_frame('name=>{}'.format(timestap))
        elif location == 'lower':
            timestap = self.folderclickAddLower()
            time.sleep(0.2)
            self.driver.switch_to_frame('name=>{}'.format(timestap))
        self.inputFoldername(foldername)
        time.sleep(0.2)
        if clicksubmit == 'y':
            self.clickAddSubmit()
        elif clicksubmit == 'n':
            self.clickAddCancle()

    def renameFolder(self, foldername, topfolderid, clicksubmit='y'):
        """点击一个文件夹后的...按钮选择重命名，
        foldername：文件名
        topfolderid:源文件夹id
        """
        self.clicktopfolderByid(topfolderid)
        time.sleep(0.2)
        self.clicktopfolderMoreByid(topfolderid)
        time.sleep(0.2)
        timestap = self.folderclickRename()
        time.sleep(0.2)
        self.driver.switch_to_frame('name=>{}'.format(timestap))
        self.inputFoldername(foldername)
        time.sleep(0.2)
        if clicksubmit == 'y':
            self.clickAddSubmit()
        elif clicksubmit == 'n':
            self.clickAddCancle()

    def returntoDefaultframe(self):
        self.driver.switch_to_frame_out()

    def mouseMoveOnClick(self, element):
        """
        鼠标悬停事件出现菜单,并点击
        element要点击的元素位置
        """
        script = '$(arguments[0]).click()'
        self.driver.js(script, element)

    def selectUploadfile(self, fpath):
        self.driver.type('css=>input[name="file"]', fpath)  # 上传文件

    def addDataFromfile(self, ftype, fpath=None, fname=None, selectfolder='y', clicksubmit=None):
        """从文件添加数据，目前只添加到最后一个文件夹中
        ftype：上传文件类型excel，csv，json
        fpath：上传文件所在路径
        fname：上传后的文件名
        folderid：上传到哪个文件夹
        selectfolder：是否选择文件夹
        clicksubmit：是否点击提交
        """
        self.driver.click('css=>#dLabel')
        # self.driver.move_to_element('css=>.dropdown-submenu>a')
        time.sleep(0.5)
        if ftype == 'excel':
            el = self.driver.get_element('css=>#uploadExcel')
        elif ftype == 'csv':
            el = self.driver.get_element('css=>#uploadText')
        elif ftype == 'json':
            el = self.driver.get_element('css=>#uploadJSON')
        self.mouseMoveOnClick(el)
        labelledby = self.driver.get_attribute(
            'css=>div[class="ui-popup ui-popup-modal ui-popup-show ui-popup-focus"]',
            'aria-labelledby')
        timestap = labelledby.split(':')[1]
        self.driver.switch_to_frame('name=>{}'.format(timestap))
        if fpath:
            self.selectUploadfile(fpath)#上传文件
        if selectfolder == 'y':
            self.driver.click('css=>#fileName')#选择文件夹
            folderid_list = self.getAllTopfolderList()
            if len(folderid_list) > 1:
                folderid = folderid_list[-1]#最后一个文件夹的id
                self.driver.click('css=>#{}_span'.format(folderid))#选择文件夹
            else:
                folderid = folderid_list[0]#最后一个文件夹得id
                self.driver.click('css=>#{}_span'.format(folderid))#选择文件夹
        self.driver.clear('css=>#name')  # 清空输入框
        if fname:
            self.driver.type('css=>#name', fname)#输入文件名称
        if clicksubmit == 'y':
            self.clickUploadSubmit()
        elif clicksubmit == 'n':
            self.clickAddCancle()


    def getSonfolderIds(self, fatherid='treeDemo_1'):
        """通过父文件夹id查询所有子文件id"""
        self.driver.click('css=>#{}_switch'.format(fatherid))
        sonfolderIds = self.driver.get_attributes(
            'css=>#{}_ul>li>a>span[style="background:url(../style/icon/class.png) 0 0 no-repeat;"]'.format(fatherid),
            'id')
        return sonfolderIds

    def getSonfileIds(self, fatherid='treeDemo_1'):
        """通过父文件夹id查询所有子文id"""
        self.driver.click('css=>#{}_switch'.format(fatherid))
        sonfileIds = self.driver.get_attributes(
            'css=>#{}_ul>li>a>span[style="background:url(../style/icon/schema.png) 0 0 no-repeat;"]'.format(fatherid),
            'id')
        return sonfileIds

    def folderclickAddSame(self):
        """点击添加同级文件夹，并返时间戳"""
        self.driver.click('css=>td[data-num="0"]')
        time.sleep(0.5)
        labelledby = self.driver.get_attribute(
            'css=>div[class="ui-popup ui-popup-modal ui-popup-show ui-popup-focus"]', 'aria-labelledby')
        timestap = labelledby.split(':')[1]
        return timestap

    def folderclickAddLower(self):
        """点击添加下级文件夹，并返时间戳"""
        self.driver.click('css=>td[data-num="1"]')
        time.sleep(0.5)
        labelledby = self.driver.get_attribute(
            'css=>div[class="ui-popup ui-popup-modal ui-popup-show ui-popup-focus"]', 'aria-labelledby')
        timestap = labelledby.split(':')[1]
        return timestap

    def folderclickRename(self):
        """点击重命名文件夹，并返时间戳"""
        self.driver.click('css=>td[data-num="3"]')
        time.sleep(0.5)
        labelledby = self.driver.get_attribute(
            'css=>div[class="ui-popup ui-popup-modal ui-popup-show ui-popup-focus"]', 'aria-labelledby')
        timestap = labelledby.split(':')[1]
        return timestap

    def addDataFromSecretary(self, access='y', fname=None, selectfolder='y', clicksubmit=None):
        """从秘书账号添加数据
        access：是否有秘书导入权限
        fname：上传后的文件名
        folderid：上传到哪个文件夹
        selectfolder：是否选择文件夹
        clicksubmit：是否点击提交
        """
        self.driver.click('css=>#dLabel')
        self.driver.click('css=>a[href="javascript:toSecretary()"]')
        if access:
            labelledby = self.driver.get_attribute(
                'css=>div[class="ui-popup ui-popup-modal ui-popup-show ui-popup-focus"]',
                'aria-labelledby')
            timestap = labelledby.split(':')[1]
            self.driver.switch_to_frame('name=>{}'.format(timestap))
            if selectfolder == 'y':
                self.driver.click('css=>#fileName')#选择文件夹
                folderid_list = self.getAllTopfolderList()
                if len(folderid_list) > 1:
                    folderid = folderid_list[-1]#最后一个文件夹的id
                    self.driver.click('css=>#{}_span'.format(folderid))#选择文件夹
                else:
                    folderid = folderid_list[0]#最后一个文件夹得id
                    self.driver.click('css=>#{}_span'.format(folderid))#选择文件夹
            self.driver.clear('css=>#schemaName')  # 清空输入框
            if fname:
                self.driver.type('css=>#schemaName', fname)#输入文件名称
            if clicksubmit == 'y':
                self.clickUploadSecretarySubmit()
            elif clicksubmit == 'n':
                self.clickAddCancle()














